<template>
  <div class="ledger-card card-item">
    <div class="ledger__header">
      <span>{{ props.item.category }}</span>
      <span>{{ formatAmount(props.item.totalAmount) }}</span>
    </div>

    <div class="ledger__body">
      <div v-for="(legerItem, index) in props.item.itemList"
           :key="index" class="ledger-item" @click="editClick(legerItem)">

        <div class="ledger-name">
          <span>{{ legerItem.content }}</span>
          <span v-if="legerItem.remark">({{ legerItem.remark }})</span>
        </div>

        <span>{{ legerItem.price }}</span>

        <div class="ledger-unit">
          <span>{{ legerItem.number }}</span>
          <span>({{ legerItem.unit }})</span>
        </div>

        <div class="ledger-payer" style="color: #4169E1;">
          <i class="fa-solid fa-flag"></i>
          <span>{{ legerItem.payer }}</span>
        </div>

        <div class="ledger-amount" style="color: #DAA520;">
          <i class="fa-solid fa-cny"></i>
          <span>{{ formatAmount(legerItem.totalPrice) }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineProps, PropType } from 'vue'
import { formatAmount } from 'vue3-common/utils/numberUtil'
import { isMobile } from 'vue3-common/utils/layoutUtil'
import { deepCopyObject } from 'vue3-common/utils/dataUtil'
import { useLedgerStore } from '@/store/ledger.ts'
import { useRouter } from 'vue-router'
import { ILedger, ILegerItem } from '@/types/ledger.ts'

const props = defineProps({
  item: {
    required: true,
    type: Object as PropType<ILedger>
  }
})

const ledgerStore = useLedgerStore()
const router = useRouter()

const editClick = async (leger: ILegerItem) => {
  ledgerStore.ledgerApiType = 'UPDATE'
  ledgerStore.currentLedgerRecord = ledgerStore.ledgerRecordList.find((item) => item.id === leger.id)

  await router.push({ name: 'LedgerDetailId', params: { id: leger.id } })
}
</script>

<style lang="scss">
.ledger-card {
  padding: 10px;

  display: grid;
  grid-template-rows: 25px 1fr;
  align-items: center;
  gap: 15px;

  .ledger__header {
    padding-bottom: 5px;
    border-bottom: 1px solid var(--el-color-primary);
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 1.2rem;
  }

  .ledger__body {
    display: flex;
    flex-direction: column;
    gap: 20px;

    .ledger-item {
      display: grid;
      grid-template-columns: 1fr 60px 50px 50px 60px;
      gap: 5px;
      color: #4F4F4F;

      .ledger-unit {
        display: flex;
        align-items: center;
        gap: 2px;
      }

      .ledger-payer, .ledger-amount {
        display: flex;
        align-items: center;
        gap: 5px;
      }
    }
  }
}
</style>
